-- area_damage3
-- create by zhuangsl
-- 技能: 范围伤害。以目标为中心，对周围n距离以内的敌人造成伤害=魔力/攻击较大值 * 威力 * 加成
-- 目前支持如下两种配置：
-- 1、damage(1200,1)：对半径1范围内目标造成1200威力的魔法伤害


return {
    apply = function(source, target, skillId, round, para, extra)
        -- 威力，半径
        local force = para[1];
        local radius = para[2];

        -- 1. 计算技能最终的威力
        local prop = PropM.combine(source, "magic_effect", skillId);
        force = force + PropM.apply(prop, force);

        -- 2. 攻击方的魔力
        local magic  = source:getMagic();
        local attack = source:getAttack();

        -- 被对方百分比削弱
        prop   = PropM.combine(target, "weak_enemy", "magic");
        magic  = PropM.apply(prop, magic);
        prop   = PropM.combine(target, "weak_enemy", "attack");
        attack = PropM.apply(prop, attack);

        -- 被对方绝对值削弱
        prop   = PropM.combine(target, "weak_enemy2", "magic");
        magic  = PropM.apply(prop, magic);
        prop   = PropM.combine(target, "weak_enemy2", "attack");
        attack = PropM.apply(prop, attack);

        -- 3. 伤害/计算时魔力和攻击的较大值
        local damage = math.modf(force * math.max(magic, attack) / 1000);

        -- 4. 遍历目标周围所有已开且存活的怪
        local pos = target.dbase:query("pos");
        if not pos then
            pos = target:getOwner();
        end
        local coor = DungeonM.convertToCoor(pos);
        local ret = {};

        for p = 1, DUNGEON_WIDTH * DUNGEON_HEIGHT do
            local c = DungeonM.convertToCoor(p);

            -- 有效范围内
            local near = math.abs(c.x - coor.x) + math.abs(c.y - coor.y) <= radius;

            local grid = DungeonM.getGridByPos(p);

            -- 翻开且存活的怪物
            if  near and grid:isOpened() and
                grid:isMonster() and
                not grid.monster:isDead() then
                table.insert(ret, grid.monster);
            end
        end

        for _, monster in pairs(ret) do
            -- 道具修正伤害
            local classId = extra["propertyId"];
            local fixDamage = damage;
            if type(classId) == "number" then
                fixDamage = PropertyM.fixPropertyDamage(source, monster, classId, fixDamage);
            end

            -- 增加命中动作
            if SkillM.getSequence(monster) == nil then
                SkillM.getSequenceList()[monster] = CastSequence.create(source, monster, skillId);
            end

            SkillM.getSequence(monster):hit(source, monster, skillId);
            -- 受创
            CombatM.receiveDamage(source, monster, fixDamage, skillId, extra);
        end

        -- 返回作用目标
        return ret;
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");

        if #arr == 2 then
            -- 规则1：area_damage(威力,半径)
            return { tonumber(arr[1]), tonumber(arr[2]) };
        else
            -- 规则2：area_damage(威力,半径,style,2000)
            return { tonumber(arr[1]), tonumber(arr[2]), tonumber(arr[3]), tonumber(arr[4]), };
        end
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        local force = para[1];
        local radius = para[2];

        -- 1. 计算机能最终的威力
        local prop = PropM.combine(source, "magic_effect", skillId);
        force = force + PropM.apply(prop, force);

        -- 2. 攻击方的魔力
        local magic = source:getMagic();
        local attack = source:getAttack();

        -- 3. 伤害
        local damage = math.modf(force * math.max(magic, attack) / 1000);

        desc = string.gsub(desc, "{area_damage3}", damage);

        return desc;
    end,
}
